import 'dart:math';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hygf/bean/CustomerList.dart';
import 'package:hygf/util/location/location_gaode.dart';
import 'package:intl/intl.dart';
import '../../../../res/HDColors.dart';
import '../../../../util/LoadingUtils.dart';
import '../../../../view/FieldDate.dart';
import '../../../../view/FieldEditor.dart';
import '../../../../view/common_widget.dart';
import '../../../../view/image_picker_widget.dart';
import '../base_msg/logic.dart';
import 'logic.dart';

class ConstructMsgPage extends StatelessWidget {
  final logic = Get.put(ConstructMsgLogic());
  final state = Get.find<ConstructMsgLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ConstructMsgLogic>(builder: (logic) {
      return SingleChildScrollView(
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          FieldDate(
              label: '材料进场时间',
              initialValue: state.constructionLink.materialsEntranceDate ?? "",
              readOnly: state.isReadOnly,
              minDateTime: DateFormat("yyyy-MM-dd HH:mm:ss")
                  .format(DateTime.now().add(const Duration(days:-365,minutes: 1))),

              onChanged: (value) {
                state.constructionLink.materialsEntranceDate = value;
              }),
          FieldDate(
              label: '施工开始时间',
              initialValue: state.constructionLink.startDate ?? "",
              readOnly: state.isReadOnly,
              minDateTime: DateFormat("yyyy-MM-dd HH:mm:ss")
                  .format(DateTime.now().add(const Duration(days:-365,minutes: 1))),

              onChanged: (value) {
                state.constructionLink.startDate = value;
              }),
          FieldDate(
              label: '施工结束时间',
              initialValue: state.constructionLink.endDate ?? "",
              readOnly: state.isReadOnly,
              minDateTime: DateFormat("yyyy-MM-dd HH:mm:ss")
                  .format(DateTime.now().add(const Duration(days:-365,minutes: 1))),

              onChanged: (value) {
                state.constructionLink.endDate = value;
              }),
          Container(
            color: HDColors.white,
            child: Stack(children: [
              FieldEditor(
                  label: '施工位置',
                  hintText: '请输入施工位置',
                  contentTextAlign: TextAlign.end,
                  initialValue: state.constructionLink.address,
                  rightIcon:
                      Icon(color: HDColors.blue, Icons.location_on_outlined),
                  showLine: false,

                  readOnly: state.isReadOnly,
                  controller: state.locationController
                    ..text = state.constructionLink.address ?? "",
                  onChanged: (value) {
                    state.constructionLink.address = value;
                  }),
              Positioned(
                right: -10,
                top: 0,
                bottom: 0,
                child: TextButton(
                  onPressed: () async {
                    LoadingUtils.showToast("开始获取当前定位");
                    var getLocation =
                        await LocationGaode().startLocation((location) {
                      if ((location.address ?? "").isEmpty) {
                        LoadingUtils.showToast("定位失败,请重新定位");
                      } else {}
                    });
                    LoadingUtils.showToast("定位:${getLocation.address}");
                    state.constructionLink.address = getLocation.address;
                    state.constructionLink.addressLongitude =
                        getLocation.longitude;
                    state.constructionLink.addressLatitude =
                        getLocation.latitude;
                    state.locationController.text =
                        state.constructionLink.address ?? "";
                    logic.update();
                  },
                  child: Container(
                    alignment: Alignment.centerRight,
                    height: 80,
                    width: 30,
                  ),
                ),
              ),
            ]),
          ),
          Visibility(
            visible: (state.constructionLink.addressLongitude ?? "").isNotEmpty,
            child: BrnTextInputFormItem(
              title: '经度：${state.constructionLink.addressLongitude}',
              isRequire: false,
              isEdit: state.isReadOnly,
              hint: "",
            ),
          ),
          Visibility(
            visible: (state.constructionLink.addressLatitude ?? "").isNotEmpty,
            child: BrnTextInputFormItem(
              title: '纬度：${state.constructionLink.addressLatitude}',
              isRequire: false,
              isEdit: state.isReadOnly,
              hint: "",
            ),
          ),
          FieldEditor(
              label: '施工人员名单',
              hintText: '请输入施工人员名单',
              contentTextAlign: TextAlign.end,
              initialValue: state.constructionLink.constructionUser,
              showLine: false,

              readOnly: state.isReadOnly,
              controller: state.constructionUserController
                ..text = state.constructionLink.constructionUser ?? "",
              onChanged: (value) {
                state.constructionLink.constructionUser = value;
              }),
          FieldEditor(
              label: '安全员姓名',
              hintText: '请输入安全员姓名',
              contentTextAlign: TextAlign.end,
              initialValue: state.constructionLink.safetyUser,
              showLine: false,

              readOnly: state.isReadOnly,
              controller: state.safetyUserController
                ..text = state.constructionLink.safetyUser ?? "",
              onChanged: (value) {
                state.constructionLink.safetyUser = value;
              }),
          Container(
            color: HDColors.white,
            child: Stack(children: [
              FieldEditor(
                  label: '组件条形码',
                  hintText: '请扫码获取信息',
                  contentTextAlign: TextAlign.end,
                  initialValue: state.constructionLink.componentNumber,
                  rightIcon: Icon(color: HDColors.blue, Icons.barcode_reader),
                  showLine: false,
                  isRequired: false,
                  readOnly: state.isReadOnly,
                  controller: state.codeController
                    ..text = state.constructionLink.componentNumber ?? "",
                  onChanged: (value) {
                    state.constructionLink.componentNumber = value;
                  }),
              Positioned(
                right: -10,
                top: 0,
                bottom: 0,
                child: TextButton(
                  onPressed: () async {
                      logic.jumpScanPage();
                  },
                  child: Container(
                    alignment: Alignment.centerRight,
                    height: 80,
                    width: 30,
                  ),
                ),
              ),
            ]),
          ),
          FieldEditor(
              label: '备注',
              hintText: '请输入备注信息',
              initialValue: state.constructionLink.remarks,
              contentTextAlign: TextAlign.end,
              showLine: false,
              isRequired: false,
              readOnly: state.isReadOnly,
              controller: state.reMarkController
                ..text = state.constructionLink.remarks ?? "",
              onChanged: (value) {
                state.constructionLink.remarks = value;
              }),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('材料进场照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.mEntrancePhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('早晚班会照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.classMeetingPhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('安全防护照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.safetyProtectionPhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('支架安装照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.stentInstallPhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('组件安装照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.componentInstallPhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('逆变器、配电箱安装照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.inverterInstallPhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('支架安装接地照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.stentInstallGroundController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('逆变器、配电箱接地照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.inverterInstallGroundController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('电缆铺设照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.cableLayingPhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('组件安装完成航拍图',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.componentFinishPhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('电阻照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.resistancePhotoController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('电压照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.voltagePhotoController),
          ),
          // Container(
          //   margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
          //   child: Text('逆变器照片',
          //       style: TextStyle(color: HDColors.black, fontSize: 16)),
          // ),
          // Container(
          //   margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
          //   child: ImagePickerWidget(
          //       readonly: state.isReadOnly,
          //       controller: state.inverterPhotoController),
          // ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('其他',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(20, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.rnOtherImgController),
          ),
          Visibility(visible: !state.isReadOnly, child: getFooterButton())
        ]),
      );
    });
  }

  getFooterButton() {
    return Container(
        decoration: const BoxDecoration(color: HDColors.white, boxShadow: [
          BoxShadow(
              color: HDColors.grayBg,
              offset: Offset(0, -2),
              blurRadius: 8,
              spreadRadius: 1)
        ]),
        padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            CommonWidget.buildBottomBtn("暂存施工信息", () {
              logic.save();
            }),
            CommonWidget.buildBottomBtn("提交施工信息", () {
              logic.submit(1);
            }),
          ],
        ));
  }

  void setData(ConstructionLink? tempData) {
    if (tempData != null) {
      state.constructionLink = tempData;
      logic.initImgUrl();
    }
  }
}
